/********************************************************************
	TABAL ENGINE - 2012/11/04
	author:		Marc Cuscullola
	
	purpose:	Template class for creating Map Managers
*********************************************************************/
#pragma once

#ifndef __UTILITIES__MAP_MANAGER__INCLUDED_H__
#define __UTILITIES__MAP_MANAGER__INCLUDED_H__

#include "Base.h"
#include <string>
#include <map>

template<class T>
class CMapManager
{
public:
	virtual T * GetResource(const std::string & _sName)
	{
		std::map<std::string, T*>::iterator it = m_Resources.find(_sName);
		if (it != m_Resources.end())
		{
			return it->second;
		}

		return NULL;
	}

	virtual bool RemoveResource(const std::string& _sName)
	{
		std::map<std::string, T*>::iterator it = m_Resources.find(_sName);
		if (it != m_Resources.end())
		{
			CHECKED_DELETE(it->second);
			m_Resources.erase(it);
			return true;
		}

		return false;
	}

	virtual bool AddResource(const std::string & _sName, T * _pResource)
	{
		if(_sName == "" )
			return false;

		if (m_Resources.find(_sName) == m_Resources.end())
		{
			m_Resources[_sName]=_pResource;
			return true;
		}

		return false;
	}

	void Destroy()
	{
		std::map<std::string, T*>::iterator it = m_Resources.begin();
		std::map<std::string, T*>::iterator itEnd = m_Resources.end();

		for(; it != itEnd; ++it)
		{
			CHECKED_DELETE(it->second);
		}

		m_Resources.clear();
	}

protected:
	typedef std::map<std::string, T*>	TMapResource;
	TMapResource						m_Resources;
};

#endif //__UTILITIES__MAP_MANAGER__INCLUDED_H__
